package com.ishanggang.controller;

import com.alibaba.excel.EasyExcel;
import com.ishanggang.entity.ShopQuechao;
import com.ishanggang.mapper.ShopQuechaoMapper;
import com.ishanggang.util.excel.IndexOrNameDataListener;
import com.ishanggang.vo.BrandShopExcelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/godHand")
@CrossOrigin
@Slf4j
public class GodHandController {
    @Resource
    private ShopQuechaoMapper shopQuechaoMapper;

    /**
     * 文件上传
     * <p>1. 创建excel对应的实体对象 参照{@link BrandShopExcelVo}
     * <p>2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link IndexOrNameDataListener}
     * <p>3. 直接读即可
     */
    @PostMapping("upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        EasyExcel
                .read(file.getInputStream(), BrandShopExcelVo.class, new IndexOrNameDataListener(shopQuechaoMapper)).sheet("门店主表").doRead();
        return "success";
    }
}
